using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Networking;

[NetworkSettings(channel = 0, sendInterval = 1f)]
public class CharacterInventory : NetworkBehaviour
{
	[HideInInspector]
	public CharacterSystem character;

	[Header("Graphics")]
	public FPSItemEquipment DefaultHand;

	public ItemSticker[] itemStickers;

	[HideInInspector]
	public List<ItemCollector> Items = new List<ItemCollector>();

	[HideInInspector]
	public PlayerView playerView;

	[HideInInspector]
	public FPSItemEquipment FPSEquipment;

	[HideInInspector]
	public ItemEquipment TDEquipment;

	[HideInInspector]
	[SyncVar(hook = "OnInventoryChanged")]
	public string StickerTextData;

	[HideInInspector]
	public bool Toggle;

	[Header("Items")]
	public ItemDataPackage[] StarterItems;

	[HideInInspector]
	[SyncVar]
	public int UpdateCount;

	public bool Limited;

	public int LimitedSlot = 30;

	[HideInInspector]
	public bool IsReady;

	[HideInInspector]
	public ItemSticker FPSObject;

	public Transform Pointer;

	public bool EquipStarter;

	public CharacterInventory PeerTrade;

	[HideInInspector]
	public ItemSticker stickerTarget;

	private ItemCollector previousPrimaryItem;

	[HideInInspector]
	public ItemCollector collectorAttachedTemp;

	private int LateUpdateCount;

	private static int kCmdCmdAddItemSync;

	private static int kCmdCmdRemoveItemSync;

	private static int kCmdCmdDropitem;

	private static int kCmdCmdPutCollector;

	private static int kCmdCmdUpdateOtherInventory;

	public string NetworkStickerTextData
	{
		get
		{
			return StickerTextData;
		}
		[param: In]
		set
		{
			ref string stickerTextData = ref StickerTextData;
			if (NetworkServer.localClientActive && !base.syncVarHookGuard)
			{
				base.syncVarHookGuard = true;
				OnInventoryChanged(value);
				base.syncVarHookGuard = false;
			}
			SetSyncVar(value, ref stickerTextData, 1u);
		}
	}

	public int NetworkUpdateCount
	{
		get
		{
			return UpdateCount;
		}
		[param: In]
		set
		{
			SetSyncVar(value, ref UpdateCount, 2u);
		}
	}

	private void Awake()
	{
		character = base.gameObject.GetComponent<CharacterSystem>();
		playerView = base.gameObject.GetComponent<PlayerView>();
		if (base.transform.GetComponentsInChildren(typeof(ItemSticker)).Length <= 0)
		{
			return;
		}
		Component[] componentsInChildren = base.transform.GetComponentsInChildren(typeof(ItemSticker));
		itemStickers = new ItemSticker[componentsInChildren.Length];
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			itemStickers[i] = componentsInChildren[i].GetComponent<ItemSticker>();
			itemStickers[i].ItemIndex = -1;
			itemStickers[i].Index = i;
			if (itemStickers[i].equipType == EquipType.FPSItemView)
			{
				FPSObject = itemStickers[i];
			}
		}
	}

	public void SetupStarterItem()
	{
		RemoveAllItem();
		int num = 0;
		for (int i = 0; i < StarterItems.Length; i++)
		{
			if (StarterItems[i].item != null)
			{
				AddItemByItemData(StarterItems[i].item, StarterItems[i].Num, -1, num);
				num++;
			}
		}
		int num2 = 0;
		while (true)
		{
			if (num2 < Items.Count)
			{
				if (Items[num2] != null && Items[num2].Item != null && Items[num2].Item.ItemEquip != null && (Items[num2].Item.ItemEquip.itemType == EquipType.PrimaryGun || Items[num2].Item.ItemEquip.itemType == EquipType.SecondaryGun || Items[num2].Item.ItemEquip.itemType == EquipType.MeleeWeapon))
				{
					break;
				}
				num2++;
				continue;
			}
			return;
		}
		if (Items[num2].Item.ItemEquip.AutoEquip || Items[num2].Item.ItemEquip.AutoToggle)
		{
			EquipItemByCollector(Items[num2]);
			ToggleUseEquipped(Items[num2]);
		}
	}

	private void Start()
	{
		if (playerView != null && playerView.FPSCamera != null)
		{
			Pointer = playerView.FPSCamera.RayPointer;
		}
		if (EquipStarter)
		{
			ApplyStarterItem();
		}
		IsReady = true;
	}

	public string GenStickerTextData()
	{
		string text = string.Empty;
		for (int i = 0; i < itemStickers.Length; i++)
		{
			string str = itemStickers[i].ItemIndex.ToString();
			if (itemStickers[i].transform.childCount <= 0 || !itemStickers[i].IsVisible)
			{
				str = "-1";
			}
			text = text + str + ",";
		}
		return text;
	}

	public string GetItemDataText()
	{
		string empty = string.Empty;
		string text = string.Empty;
		string text2 = string.Empty;
		string text3 = string.Empty;
		string text4 = string.Empty;
		foreach (ItemCollector item in Items)
		{
			if (item.Item != null)
			{
				text = text + item.Item.ItemID + ",";
				text2 = text2 + item.Num + ",";
				text3 = text3 + item.NumTag + ",";
				text4 = text4 + item.Shortcut + ",";
			}
		}
		return text + "|" + text2 + "|" + text3 + "|" + text4;
	}

	public void SetItemsFromText(string itemdatatext)
	{
		NetworkUpdateCount = UpdateCount + 1;
		ItemManager itemManager = (ItemManager)Object.FindObjectOfType(typeof(ItemManager));
		if (!itemManager)
		{
			return;
		}
		string[] array = itemdatatext.Split("|"[0]);
		if (array.Length < 4)
		{
			return;
		}
		RemoveAllItem();
		string[] array2 = array[0].Split(","[0]);
		string[] array3 = array[1].Split(","[0]);
		string[] array4 = array[2].Split(","[0]);
		string[] array5 = array[3].Split(","[0]);
		for (int i = 0; i < array2.Length; i++)
		{
			if (array2[i] != string.Empty)
			{
				ItemCollector itemCollector = new ItemCollector();
				itemCollector.Item = itemManager.GetItemDataByID(array2[i]);
				if (itemCollector.Item != null)
				{
					int.TryParse(array3[i], out itemCollector.Num);
					int.TryParse(array4[i], out itemCollector.NumTag);
					int.TryParse(array5[i], out itemCollector.Shortcut);
					itemCollector.Active = true;
					AddItemByItemDataNoLimit(itemCollector.Item, itemCollector.Num, itemCollector.NumTag, itemCollector.Shortcut);
				}
			}
		}
	}

	public void AutoEquiping(ItemData itemdata)
	{
		for (int i = 0; i < Items.Count; i++)
		{
			if (Items[i].Item != null && Items[i].Item.ItemID == itemdata.ItemID && Items[i].Item.ItemEquip != null && Items[i].Item.ItemEquip.AutoEquip)
			{
				EquipItemByCollector(Items[i]);
			}
		}
	}

	public bool DropItemBySameEquipType(ItemData itemdata)
	{
		int num = 0;
		for (int i = 0; i < itemStickers.Length; i++)
		{
			if (itemStickers[i] != null && itemdata.ItemEquip != null && itemStickers[i].equipType == itemdata.ItemEquip.itemType)
			{
				num++;
			}
		}
		for (int j = 0; j < Items.Count; j++)
		{
			if (Items[j] != null && Items[j].Item != null && Items[j].Item.UniqueItem && Items[j].Item.ItemEquip != null && itemdata.ItemEquip != null && Items[j].Item.ItemEquip.itemType == itemdata.ItemEquip.itemType && Items[j].EquipIndex == -1)
			{
				UnityEngine.Debug.Log("Drop from inventory " + Items[j].Item.ItemName);
				DropItemByCollector(Items[j], Items[j].Num);
				return true;
			}
		}
		int num2 = 0;
		for (int k = 0; k < Items.Count; k++)
		{
			if (Items[k] != null && Items[k].Item != null && Items[k].Item.UniqueItem && Items[k].Item.ItemEquip != null && itemdata.ItemEquip != null && Items[k].Item.ItemEquip.itemType == itemdata.ItemEquip.itemType)
			{
				num2++;
				if (num2 >= num && Items[k].EquipIndex != -1)
				{
					UnityEngine.Debug.Log("Drop from slot " + Items[k].Item.ItemName);
					DropItemByCollector(Items[k], Items[k].Num);
					return true;
				}
			}
		}
		return false;
	}

	public bool DropItemBySticker(ItemSticker sticker)
	{
		for (int i = 0; i < Items.Count; i++)
		{
			if (Items[i] != null && Items[i].Item != null && Items[i].EquipIndex == sticker.Index)
			{
				DropItemByCollector(Items[i], Items[i].Num);
				return true;
			}
		}
		return false;
	}

	public void AddItemFromText(string itemdatatext)
	{
		NetworkUpdateCount = UpdateCount + 1;
		ItemManager itemManager = (ItemManager)Object.FindObjectOfType(typeof(ItemManager));
		if (!itemManager)
		{
			return;
		}
		string[] array = itemdatatext.Split("|"[0]);
		string[] array2 = array[0].Split(","[0]);
		string[] array3 = array[1].Split(","[0]);
		string[] array4 = array[2].Split(","[0]);
		for (int i = 0; i < array2.Length; i++)
		{
			if (array2[i] != string.Empty)
			{
				ItemCollector itemCollector = new ItemCollector();
				itemCollector.Item = itemManager.GetItemDataByID(array2[i]);
				int.TryParse(array3[i], out itemCollector.Num);
				int.TryParse(array4[i], out itemCollector.NumTag);
				itemCollector.Active = true;
				AddItemByItemDataNoLimit(itemCollector.Item, itemCollector.Num, itemCollector.NumTag, -1);
			}
		}
	}

	public bool AddItemTest(ItemCollector item, int num)
	{
		if (UnitZ.itemManager != null && item.Item != null && num > 0)
		{
			ItemData itemData = UnitZ.itemManager.CloneItemData(item.Item);
			if (itemData != null)
			{
				return AddItemTest(itemData, num);
			}
		}
		return false;
	}

	public bool AddItemTest(ItemData itemdata, int num)
	{
		if (UnitZ.itemManager != null && itemdata != null && num > 0)
		{
			for (int i = 0; i < Items.Count; i++)
			{
				if (Items[i].Item != null && Items[i].Item.ItemID == itemdata.ItemID && itemdata.Stack)
				{
					return true;
				}
			}
			int num2 = 0;
			for (int j = 0; j < Items.Count; j++)
			{
				if (Items[j] != null && Items[j].Item != null)
				{
					num2++;
				}
			}
			if (Limited && num2 >= LimitedSlot)
			{
				return false;
			}
			return true;
		}
		return false;
	}

	public ItemCollector AddItemByIndex(int index, int num, int numtag)
	{
		NetworkUpdateCount = UpdateCount + 1;
		if (UnitZ.itemManager != null)
		{
			ItemData item = UnitZ.itemManager.GetItem(index);
			if (item != null && num > 0)
			{
				for (int i = 0; i < Items.Count; i++)
				{
					if (Items[i].ItemIndex == index && item.Stack)
					{
						Items[i].Num += num;
						return Items[i];
					}
				}
				int num2 = 0;
				for (int j = 0; j < Items.Count; j++)
				{
					if (Items[j] != null && Items[j].Item != null)
					{
						num2++;
					}
				}
				if (Limited && num2 >= LimitedSlot)
				{
					return null;
				}
				if (item.Stack)
				{
					ItemCollector itemCollector = new ItemCollector();
					itemCollector.ItemIndex = index;
					itemCollector.Item = item;
					itemCollector.NumTag = numtag;
					itemCollector.Num += num;
					Items.Add(itemCollector);
					if (itemCollector.Item != null && itemCollector.Item.ItemEquip != null && (itemCollector.Item.ItemEquip.AutoEquip || itemCollector.Item.ItemEquip.AutoToggle))
					{
						EquipItemByCollector(itemCollector);
					}
					return itemCollector;
				}
				if (num > 0)
				{
					num--;
					ItemCollector itemCollector2 = new ItemCollector();
					itemCollector2.ItemIndex = index;
					itemCollector2.Item = item;
					itemCollector2.NumTag = numtag;
					itemCollector2.Num = 1;
					Items.Add(itemCollector2);
					if (itemCollector2.Item != null && itemCollector2.Item.ItemEquip != null && (itemCollector2.Item.ItemEquip.AutoEquip || itemCollector2.Item.ItemEquip.AutoToggle))
					{
						EquipItemByCollector(itemCollector2);
					}
					return itemCollector2;
				}
			}
		}
		return null;
	}

	public ItemCollector AddItemByItemDataNoLimit(ItemData item, int num, int numtag, int shortcut)
	{
		NetworkUpdateCount = UpdateCount + 1;
		if (UnitZ.itemManager != null && item != null && num > 0)
		{
			ItemData itemData = UnitZ.itemManager.CloneItemData(item);
			if (itemData != null)
			{
				for (int i = 0; i < Items.Count; i++)
				{
					if (Items[i].Item != null && Items[i].Item.ItemID == itemData.ItemID && itemData.Stack)
					{
						Items[i].Num += num;
						return Items[i];
					}
				}
				if (itemData.Stack)
				{
					ItemCollector itemCollector = new ItemCollector();
					itemCollector.ItemIndex = UnitZ.itemManager.GetIndexByID(itemData.ItemID);
					itemCollector.Item = itemData;
					itemCollector.NumTag = numtag;
					itemCollector.Shortcut = shortcut;
					itemCollector.Num += num;
					Items.Add(itemCollector);
					return itemCollector;
				}
				if (num > 0)
				{
					num--;
					ItemCollector itemCollector2 = new ItemCollector();
					itemCollector2.ItemIndex = UnitZ.itemManager.GetIndexByID(itemData.ItemID);
					itemCollector2.Item = itemData;
					itemCollector2.NumTag = numtag;
					itemCollector2.Shortcut = shortcut;
					itemCollector2.Num = 1;
					Items.Add(itemCollector2);
					return itemCollector2;
				}
			}
		}
		return null;
	}

	public ItemCollector AddItemByItemData(ItemData item, int num, int numtag, int shortcut)
	{
		NetworkUpdateCount = UpdateCount + 1;
		if (UnitZ.itemManager != null && item != null && num > 0)
		{
			ItemData itemData = UnitZ.itemManager.CloneItemData(item);
			if (itemData != null)
			{
				for (int i = 0; i < Items.Count; i++)
				{
					if (Items[i].Item != null && Items[i].Item.ItemID == itemData.ItemID && itemData.Stack)
					{
						Items[i].Num += num;
						return Items[i];
					}
				}
				int num2 = 0;
				for (int j = 0; j < Items.Count; j++)
				{
					if (Items[j] != null && Items[j].Item != null)
					{
						num2++;
					}
				}
				if (Limited && num2 >= LimitedSlot)
				{
					return null;
				}
				if (itemData.Stack)
				{
					ItemCollector itemCollector = new ItemCollector();
					itemCollector.ItemIndex = UnitZ.itemManager.GetIndexByID(itemData.ItemID);
					itemCollector.Item = itemData;
					itemCollector.NumTag = numtag;
					itemCollector.Shortcut = shortcut;
					itemCollector.EquipIndex = -1;
					itemCollector.Num += num;
					Items.Add(itemCollector);
					if (itemCollector.Item != null && itemCollector.Item.ItemEquip != null && itemCollector.Item.ItemEquip.AutoEquip)
					{
						EquipItemByCollector(itemCollector);
					}
					return itemCollector;
				}
				if (num > 0)
				{
					num--;
					ItemCollector itemCollector2 = new ItemCollector();
					itemCollector2.ItemIndex = UnitZ.itemManager.GetIndexByID(itemData.ItemID);
					itemCollector2.Item = itemData;
					itemCollector2.NumTag = numtag;
					itemCollector2.Shortcut = shortcut;
					itemCollector2.Num = 1;
					itemCollector2.EquipIndex = -1;
					Items.Add(itemCollector2);
					if (itemCollector2.Item != null && itemCollector2.Item.ItemEquip != null && itemCollector2.Item.ItemEquip.AutoEquip)
					{
						EquipItemByCollector(itemCollector2);
					}
					return itemCollector2;
				}
			}
		}
		return null;
	}

	public ItemCollector AddItemByCollector(ItemCollector item)
	{
		NetworkUpdateCount = UpdateCount + 1;
		if (UnitZ.itemManager != null && item.Item != null && item.Num > 0)
		{
			ItemData itemData = UnitZ.itemManager.CloneItemData(item.Item);
			if (itemData != null)
			{
				for (int i = 0; i < Items.Count; i++)
				{
					if (Items[i].Item != null && Items[i].Item.ItemID == itemData.ItemID && itemData.Stack)
					{
						Items[i].Num += item.Num;
						return Items[i];
					}
				}
				int num = 0;
				for (int j = 0; j < Items.Count; j++)
				{
					if (Items[j] != null && Items[j].Item != null)
					{
						num++;
					}
				}
				if (Limited && num >= LimitedSlot)
				{
					return null;
				}
				if (itemData.Stack)
				{
					ItemCollector itemCollector = new ItemCollector();
					itemCollector.ItemIndex = UnitZ.itemManager.GetIndexByID(itemData.ItemID);
					itemCollector.Item = itemData;
					itemCollector.NumTag = item.NumTag;
					itemCollector.Shortcut = item.Shortcut;
					itemCollector.Num += item.Num;
					Items.Add(itemCollector);
					if (itemCollector.Item != null && itemCollector.Item.ItemEquip != null && (itemCollector.Item.ItemEquip.AutoEquip || itemCollector.Item.ItemEquip.AutoToggle))
					{
						EquipItemByCollector(itemCollector);
					}
					return itemCollector;
				}
				int num2 = item.Num;
				if (num2 > 0)
				{
					num2--;
					ItemCollector itemCollector2 = new ItemCollector();
					itemCollector2.ItemIndex = UnitZ.itemManager.GetIndexByID(itemData.ItemID);
					itemCollector2.Item = itemData;
					itemCollector2.NumTag = item.NumTag;
					itemCollector2.Shortcut = item.Shortcut;
					itemCollector2.Num = 1;
					Items.Add(itemCollector2);
					if (itemCollector2.Item != null && itemCollector2.Item.ItemEquip != null && (itemCollector2.Item.ItemEquip.AutoEquip || itemCollector2.Item.ItemEquip.AutoToggle))
					{
						EquipItemByCollector(itemCollector2);
					}
					return itemCollector2;
				}
			}
		}
		return null;
	}

	public ItemCollector AddItemByCollector(ItemCollector item, int num, int shortcut)
	{
		NetworkUpdateCount = UpdateCount + 1;
		if (UnitZ.itemManager != null && item.Item != null && num > 0)
		{
			ItemData itemData = UnitZ.itemManager.CloneItemData(item.Item);
			if (itemData != null)
			{
				for (int i = 0; i < Items.Count; i++)
				{
					if (Items[i].Item != null && Items[i].Item.ItemID == itemData.ItemID && itemData.Stack)
					{
						Items[i].Num += num;
						return Items[i];
					}
				}
				int num2 = 0;
				for (int j = 0; j < Items.Count; j++)
				{
					if (Items[j] != null && Items[j].Item != null)
					{
						num2++;
					}
				}
				if (Limited && num2 >= LimitedSlot)
				{
					return null;
				}
				if (itemData.Stack)
				{
					ItemCollector itemCollector = new ItemCollector();
					itemCollector.ItemIndex = UnitZ.itemManager.GetIndexByID(itemData.ItemID);
					itemCollector.Item = itemData;
					itemCollector.NumTag = item.NumTag;
					itemCollector.Shortcut = shortcut;
					itemCollector.Num += num;
					Items.Add(itemCollector);
					if (itemCollector.Item != null && itemCollector.Item.ItemEquip != null && (itemCollector.Item.ItemEquip.AutoEquip || itemCollector.Item.ItemEquip.AutoToggle))
					{
						EquipItemByCollector(itemCollector);
					}
					return itemCollector;
				}
				if (num > 0)
				{
					num--;
					ItemCollector itemCollector2 = new ItemCollector();
					itemCollector2.ItemIndex = UnitZ.itemManager.GetIndexByID(itemData.ItemID);
					itemCollector2.Item = itemData;
					itemCollector2.NumTag = item.NumTag;
					itemCollector2.Shortcut = shortcut;
					itemCollector2.Num = 1;
					Items.Add(itemCollector2);
					if (itemCollector2.Item != null && itemCollector2.Item.ItemEquip != null && (itemCollector2.Item.ItemEquip.AutoEquip || itemCollector2.Item.ItemEquip.AutoToggle))
					{
						EquipItemByCollector(itemCollector2);
					}
					return itemCollector2;
				}
			}
		}
		return null;
	}

	public ItemCollector AddItemByCollector(ItemCollector item, int num)
	{
		NetworkUpdateCount = UpdateCount + 1;
		if (UnitZ.itemManager != null && item.Item != null && num > 0)
		{
			ItemData itemData = UnitZ.itemManager.CloneItemData(item.Item);
			if (itemData != null)
			{
				for (int i = 0; i < Items.Count; i++)
				{
					if (Items[i].Item != null && Items[i].Item.ItemID == itemData.ItemID && itemData.Stack)
					{
						Items[i].Num += num;
						return Items[i];
					}
				}
				int num2 = 0;
				for (int j = 0; j < Items.Count; j++)
				{
					if (Items[j] != null && Items[j].Item != null)
					{
						num2++;
					}
				}
				if (Limited && num2 >= LimitedSlot)
				{
					return null;
				}
				if (itemData.Stack)
				{
					ItemCollector itemCollector = new ItemCollector();
					itemCollector.ItemIndex = UnitZ.itemManager.GetIndexByID(itemData.ItemID);
					itemCollector.Item = itemData;
					itemCollector.NumTag = item.NumTag;
					itemCollector.Shortcut = item.Shortcut;
					itemCollector.Num += num;
					Items.Add(itemCollector);
					if (itemCollector.Item != null && itemCollector.Item.ItemEquip != null && (itemCollector.Item.ItemEquip.AutoEquip || itemCollector.Item.ItemEquip.AutoToggle))
					{
						EquipItemByCollector(itemCollector);
					}
					return itemCollector;
				}
				if (num > 0)
				{
					num--;
					ItemCollector itemCollector2 = new ItemCollector();
					itemCollector2.ItemIndex = UnitZ.itemManager.GetIndexByID(itemData.ItemID);
					itemCollector2.Item = itemData;
					itemCollector2.NumTag = item.NumTag;
					itemCollector2.Shortcut = item.Shortcut;
					itemCollector2.Num = 1;
					Items.Add(itemCollector2);
					if (itemCollector2.Item != null && itemCollector2.Item.ItemEquip != null && (itemCollector2.Item.ItemEquip.AutoEquip || itemCollector2.Item.ItemEquip.AutoToggle))
					{
						EquipItemByCollector(itemCollector2);
					}
					return itemCollector2;
				}
			}
		}
		return null;
	}

	[Command(channel = 0)]
	private void CmdAddItemSync(string itemid, int num, int numtag, int shortcut)
	{
		UnityEngine.Debug.Log("***Reciev Trad from " + itemid);
		ItemData itemData = UnitZ.itemManager.CloneItemDataByIndex(itemid);
		if ((bool)itemData)
		{
			PeerTrade.AddItemByItemData(itemData, num, numtag, shortcut);
		}
	}

	[Command(channel = 0)]
	private void CmdRemoveItemSync(int index, int num)
	{
		UnityEngine.Debug.Log("Remove " + PeerTrade.name + " index " + index);
		PeerTrade.RemoveItemByCollectorIndex(index, num);
	}

	public void AddItemByCollectorSync(ItemCollector item)
	{
		CallCmdAddItemSync(item.Item.ItemID, item.Num, item.NumTag, item.Shortcut);
	}

	public void AddItemByCollectorSync(ItemCollector item, int num, int shortcut)
	{
		CallCmdAddItemSync(item.Item.ItemID, num, item.NumTag, shortcut);
	}

	public void RemoveItemByCollectorSync(ItemCollector itemcollector, int num)
	{
		int index = -1;
		for (int i = 0; i < PeerTrade.Items.Count; i++)
		{
			if (PeerTrade.Items[i] != null && PeerTrade.Items[i] == itemcollector)
			{
				index = i;
				break;
			}
		}
		CallCmdRemoveItemSync(index, num);
	}

	public bool RemoveItemByCollectorIndex(int index, int num)
	{
		NetworkUpdateCount = UpdateCount + 1;
		if (UnitZ.itemManager != null && num > 0)
		{
			for (int i = 0; i < Items.Count; i++)
			{
				if (Items[i] == null || i != index)
				{
					continue;
				}
				if (Items[i].Num <= 0)
				{
					UnityEngine.Debug.Log(Items[i].Item.ItemName + " Is no more");
					return false;
				}
				if (Items[i].Num < num)
				{
					if (Items[i].Num > 0)
					{
						Items[i].Num -= Items[i].Num;
					}
				}
				else
				{
					Items[i].Num -= num;
				}
				if (Items[i].Num <= 0)
				{
					RemoveEquipItemByCollector(Items[i]);
					Items.RemoveAt(index);
				}
				return true;
			}
		}
		return false;
	}

	public bool RemoveItem(ItemData itemdata, int num)
	{
		NetworkUpdateCount = UpdateCount + 1;
		if (UnitZ.itemManager != null && itemdata != null && num > 0)
		{
			for (int i = 0; i < Items.Count; i++)
			{
				if (Items[i] == null || !(Items[i].Item.ItemID == itemdata.ItemID))
				{
					continue;
				}
				if (Items[i].Num <= 0)
				{
					UnityEngine.Debug.Log(Items[i].Item.ItemName + " Is no more");
					return false;
				}
				if (Items[i].Num < num)
				{
					if (Items[i].Num > 0)
					{
						Items[i].Num -= Items[i].Num;
					}
				}
				else
				{
					Items[i].Num -= num;
				}
				if (Items[i].Num <= 0)
				{
					RemoveEquipItemByCollector(Items[i]);
					Items.RemoveAt(i);
				}
				return true;
			}
		}
		return false;
	}

	public bool RemoveItemByCollector(ItemCollector itemcollector, int num)
	{
		NetworkUpdateCount = UpdateCount + 1;
		ItemData item = itemcollector.Item;
		if (UnitZ.itemManager != null && item != null && num > 0)
		{
			for (int i = 0; i < Items.Count; i++)
			{
				if (Items[i] == null || Items[i] != itemcollector)
				{
					continue;
				}
				if (Items[i].Num <= 0)
				{
					UnityEngine.Debug.Log(Items[i].Item.ItemName + " Is no more");
					return false;
				}
				if (Items[i].Num < num)
				{
					if (Items[i].Num > 0)
					{
						Items[i].Num -= Items[i].Num;
					}
				}
				else
				{
					Items[i].Num -= num;
				}
				if (Items[i].Num <= 0)
				{
					RemoveEquipItemByCollector(Items[i]);
					Items.RemoveAt(i);
				}
				return true;
			}
		}
		return false;
	}

	public bool RemoveItemByIndex(int index, int num)
	{
		NetworkUpdateCount = UpdateCount + 1;
		if (UnitZ.itemManager != null)
		{
			ItemData item = UnitZ.itemManager.GetItem(index);
			if (item != null && num > 0)
			{
				for (int i = 0; i < Items.Count; i++)
				{
					if (Items[i] == null || Items[i].ItemIndex != index)
					{
						continue;
					}
					if (Items[i].Num <= 0)
					{
						return false;
					}
					if (Items[i].Num < num)
					{
						if (Items[i].Num > 0)
						{
							Items[i].Num -= Items[i].Num;
						}
					}
					else
					{
						Items[i].Num -= num;
					}
					if (Items[i].Num <= 0)
					{
						RemoveEquipItemByCollector(Items[i]);
						Items.RemoveAt(i);
					}
					return true;
				}
			}
		}
		return false;
	}

	public void RemoveAllItem()
	{
		NetworkUpdateCount = UpdateCount + 1;
		if (UnitZ.itemManager != null)
		{
			for (int i = 0; i < Items.Count; i++)
			{
				if (Items[i] != null)
				{
					Items[i].Num = 0;
					RemoveEquipItemByCollector(Items[i]);
					Items.RemoveAt(i);
				}
			}
		}
		Items.Clear();
	}

	public int GetItemNum(ItemData itemdata)
	{
		if (UnitZ.itemManager != null && itemdata != null)
		{
			for (int i = 0; i < Items.Count; i++)
			{
				if (Items[i].Item.ItemID == itemdata.ItemID)
				{
					return Items[i].Num;
				}
			}
		}
		return 0;
	}

	public int GetItemNumByIndex(int index)
	{
		if (UnitZ.itemManager != null)
		{
			ItemData item = UnitZ.itemManager.GetItem(index);
			if (item != null)
			{
				for (int i = 0; i < Items.Count; i++)
				{
					if (Items[i].ItemIndex == index)
					{
						return Items[i].Num;
					}
				}
			}
		}
		return 0;
	}

	public bool CheckItem(ItemData itemdata, int num)
	{
		if (UnitZ.itemManager != null && itemdata != null && num > 0)
		{
			for (int i = 0; i < Items.Count; i++)
			{
				if (Items[i].Item.ItemID == itemdata.ItemID && Items[i].Num >= num)
				{
					return true;
				}
			}
		}
		return false;
	}

	public bool CheckItemByIndex(int index, int num)
	{
		if (UnitZ.itemManager != null)
		{
			ItemData item = UnitZ.itemManager.GetItem(index);
			if (item != null && num > 0)
			{
				for (int i = 0; i < Items.Count; i++)
				{
					if (Items[i].ItemIndex == index && Items[i].Num >= num)
					{
						return true;
					}
				}
			}
		}
		return false;
	}

	[Command(channel = 0)]
	private void CmdDropitem(string itemid, int numtag, int num, Vector3 position, Quaternion rotation)
	{
		GameObject gameObject = UnitZ.itemManager.GetItemDataByID(itemid).gameObject;
		if (gameObject != null)
		{
			UnitZ.gameNetwork.RequestSpawnItem(gameObject.gameObject, numtag, num, position, rotation);
		}
	}

	public void DropItem(ItemData itemdata, int num, int numtag)
	{
		NetworkUpdateCount = UpdateCount + 1;
		if (RemoveItem(itemdata, num))
		{
			CallCmdDropitem(itemdata.ItemID, numtag, num, base.transform.position, itemdata.gameObject.transform.rotation);
		}
	}

	public void DropItemByCollector(ItemCollector item, int num)
	{
		NetworkUpdateCount = UpdateCount + 1;
		if (RemoveItemByCollector(item, num))
		{
			CallCmdDropitem(item.Item.ItemID, item.NumTag, num, base.transform.position, item.Item.gameObject.transform.rotation);
		}
	}

	public void OnViewChanged()
	{
		RemoveEquippedItem(FPSObject);
		ToggleInUseEquipped();
	}

	public void ToggleInUseEquipped()
	{
		for (int i = 0; i < Items.Count; i++)
		{
			if (Items[i] != null && Items[i].InUse)
			{
				ToggleUseEquipped(Items[i]);
			}
		}
	}

	public void ToggleUseEquipped(ItemCollector collector)
	{
		if (collector != null && UnitZ.itemManager != null)
		{
			ItemData item = UnitZ.itemManager.GetItem(collector.ItemIndex);
			if (item != null && (bool)item.ItemFPS)
			{
				EquipPrimaryItem(collector);
				AttachFPSItemViewAndCollector(item.ItemFPS, collector);
				NetworkUpdateCount = UpdateCount + 1;
			}
		}
	}

	public bool EquipItemToStickerByCollector(ItemCollector itemCollector, string Tag)
	{
		if (itemCollector.ItemIndex != -1 && itemCollector.Num > 0)
		{
			return EquipItem(itemCollector, Tag);
		}
		return false;
	}

	public bool EquipItemToStickerByCollector(ItemCollector itemCollector, ItemSticker sticker)
	{
		if (itemCollector != null && itemCollector.Item != null && (bool)itemCollector.Item.ItemEquip)
		{
			ItemEquipment itemEquip = itemCollector.Item.ItemEquip;
			if (sticker.equipType == itemEquip.itemType)
			{
				NetworkUpdateCount = UpdateCount + 1;
				clearEquippedIndex(sticker.Index);
				itemCollector.EquipIndex = sticker.Index;
				AttachSticker(sticker, itemEquip);
				sticker.ItemIndex = itemCollector.ItemIndex;
				sticker.itemCollector = itemCollector;
				return true;
			}
		}
		return false;
	}

	public void EquipItemByItemIndex(int index)
	{
		if (UnitZ.itemManager != null)
		{
			ItemCollector itemCollectorByItemIndex = GetItemCollectorByItemIndex(index);
			if (itemCollectorByItemIndex != null)
			{
				EquipItem(itemCollectorByItemIndex);
			}
		}
	}

	public void EquipItemByCollector(ItemCollector itemCollector)
	{
		if (itemCollector.ItemIndex != -1 && itemCollector.Num > 0)
		{
			EquipItem(itemCollector);
		}
	}

	public void EquipPrimaryItem(ItemCollector itemCollector)
	{
		if (itemCollector.Item == null || itemCollector.Item.ItemEquip == null)
		{
			return;
		}
		ItemEquipment itemEquip = itemCollector.Item.ItemEquip;
		int num = 0;
		while (true)
		{
			if (num < itemStickers.Length)
			{
				if (itemStickers[num] != null && itemEquip != null && itemStickers[num].equipType == EquipType.PrimaryUse)
				{
					break;
				}
				num++;
				continue;
			}
			return;
		}
		if (itemCollector.Item.SavePreviousUse)
		{
			previousPrimaryItem = itemCollector;
		}
		NetworkUpdateCount = UpdateCount + 1;
		clearUsedEquipped();
		itemCollector.InUse = true;
		AttachSticker(itemStickers[num], itemEquip);
		itemStickers[num].ItemIndex = itemCollector.ItemIndex;
		itemStickers[num].itemCollector = itemCollector;
		UpdatePrimaryInUse();
	}

	public void UpdatePrimaryInUse()
	{
		for (int i = 0; i < itemStickers.Length; i++)
		{
			if (itemStickers[i].equipType != EquipType.PrimaryUse && itemStickers[i].equipType != EquipType.FPSItemView && itemStickers[i].itemCollector != null)
			{
				itemStickers[i].Visible(!itemStickers[i].itemCollector.InUse);
			}
		}
	}

	public void EquipPreviousPrimary()
	{
		if (previousPrimaryItem != null)
		{
			ToggleUseEquipped(previousPrimaryItem);
		}
	}

	public void EquipItem(ItemCollector itemCollector)
	{
		if (itemCollector == null || !(itemCollector.Item != null) || !itemCollector.Item.ItemEquip)
		{
			return;
		}
		ItemEquipment itemEquip = itemCollector.Item.ItemEquip;
		int num = -1;
		for (int i = 0; i < itemStickers.Length; i++)
		{
			if (itemStickers[i] != null && itemEquip != null && itemStickers[i].equipType == itemEquip.itemType && itemStickers[i].IsEmpty())
			{
				num = i;
				break;
			}
		}
		if (num == -1)
		{
			for (int j = 0; j < itemStickers.Length; j++)
			{
				if (itemStickers[j] != null && itemEquip != null && itemStickers[j].equipType == itemEquip.itemType)
				{
					num = j;
				}
			}
		}
		if (num != -1)
		{
			NetworkUpdateCount = UpdateCount + 1;
			clearEquippedIndex(num);
			itemCollector.EquipIndex = num;
			AttachSticker(itemStickers[num], itemEquip);
			itemStickers[num].ItemIndex = itemCollector.ItemIndex;
			itemStickers[num].itemCollector = itemCollector;
			if (itemEquip.AutoToggle)
			{
				ToggleUseEquipped(itemCollector);
			}
		}
	}

	public void UnEquipItem(ItemCollector itemCollector)
	{
		if (itemCollector != null)
		{
			RemoveEquipItemByCollector(itemCollector);
		}
	}

	public bool EquipItem(ItemCollector itemCollector, string tag)
	{
		if (itemCollector != null && itemCollector.Item != null && (bool)itemCollector.Item.ItemEquip)
		{
			ItemEquipment itemEquip = itemCollector.Item.ItemEquip;
			for (int i = 0; i < itemStickers.Length; i++)
			{
				if (itemStickers[i] != null && itemEquip != null && itemStickers[i].Tag == tag && itemStickers[i].equipType == itemEquip.itemType)
				{
					NetworkUpdateCount = UpdateCount + 1;
					clearEquippedIndex(i);
					itemCollector.EquipIndex = i;
					AttachSticker(itemStickers[i], itemEquip);
					itemStickers[i].ItemIndex = itemCollector.ItemIndex;
					itemStickers[i].itemCollector = itemCollector;
					return true;
				}
			}
		}
		return false;
	}

	public ItemSticker GetItemSticker(EquipType Type, string tag)
	{
		for (int i = 0; i < itemStickers.Length; i++)
		{
			if (itemStickers[i] != null && itemStickers[i].equipType == Type && itemStickers[i].Tag == tag)
			{
				return itemStickers[i];
			}
		}
		return null;
	}

	public void SwapEquppedSticker(ItemSticker sticker1, ItemSticker sticker2)
	{
		ItemCollector itemCollectorByEquipIndex = GetItemCollectorByEquipIndex(sticker1.Index);
		ItemCollector itemCollectorByEquipIndex2 = GetItemCollectorByEquipIndex(sticker2.Index);
		int equipIndex = sticker1.Index;
		if (itemCollectorByEquipIndex != null)
		{
			equipIndex = itemCollectorByEquipIndex.EquipIndex;
		}
		if (itemCollectorByEquipIndex != null && itemCollectorByEquipIndex.Item.ItemEquip.itemType == sticker2.equipType)
		{
			if (itemCollectorByEquipIndex2 != null)
			{
				itemCollectorByEquipIndex.EquipIndex = itemCollectorByEquipIndex2.EquipIndex;
			}
			AttachSticker(sticker2, itemCollectorByEquipIndex.Item.ItemEquip);
			sticker2.ItemIndex = itemCollectorByEquipIndex.ItemIndex;
			sticker2.itemCollector = itemCollectorByEquipIndex;
		}
		if (itemCollectorByEquipIndex2 != null && itemCollectorByEquipIndex2.Item.ItemEquip.itemType == sticker1.equipType)
		{
			itemCollectorByEquipIndex2.EquipIndex = equipIndex;
			AttachSticker(sticker1, itemCollectorByEquipIndex2.Item.ItemEquip);
			sticker1.ItemIndex = itemCollectorByEquipIndex2.ItemIndex;
			sticker1.itemCollector = itemCollectorByEquipIndex2;
		}
		RemoveEquippedItem(FPSObject);
		ToggleInUseEquipped();
	}

	public ItemSticker GetStickerByItemCollector(ItemCollector collector)
	{
		for (int i = 0; i < itemStickers.Length; i++)
		{
			if (i == collector.EquipIndex)
			{
				return itemStickers[i];
			}
		}
		return null;
	}

	private void clearEquippedIndex(int slot)
	{
		for (int i = 0; i < Items.Count; i++)
		{
			if (Items[i] != null && Items[i].EquipIndex == slot)
			{
				Items[i].EquipIndex = -1;
			}
		}
	}

	private void clearUsedEquipped()
	{
		for (int i = 0; i < Items.Count; i++)
		{
			if (Items[i] != null)
			{
				Items[i].InUse = false;
			}
		}
	}

	public void AttachSticker(ItemSticker sticker, ItemEquipment equip)
	{
		if (sticker != null && equip != null)
		{
			RemoveSticker(sticker);
			Quaternion rotation = sticker.transform.rotation;
			rotation.eulerAngles += sticker.RotationOffset;
			GameObject gameObject = Object.Instantiate(equip.gameObject, sticker.transform.position, rotation);
			gameObject.transform.parent = sticker.gameObject.transform;
			ItemEquipment component = gameObject.GetComponent<ItemEquipment>();
			if (sticker.equipType == EquipType.PrimaryUse)
			{
				TDEquipment = component;
			}
			sticker.SendMessage("OnAttached", gameObject, SendMessageOptions.DontRequireReceiver);
			if (equip.UsingType >= 0)
			{
				base.gameObject.SendMessage("OnEquipChanged", equip.UsingType, SendMessageOptions.DontRequireReceiver);
			}
			NetworkUpdateCount = UpdateCount + 1;
		}
	}

	public void AttachFPSItemView(FPSItemEquipment item)
	{
		if (item != null && FPSObject != null)
		{
			NetworkUpdateCount = UpdateCount + 1;
			Quaternion rotation = FPSObject.transform.rotation;
			rotation.eulerAngles += FPSObject.RotationOffset;
			RemoveEquippedItem(FPSObject);
			GameObject gameObject = Object.Instantiate(item.gameObject, FPSObject.transform.position, rotation);
			gameObject.transform.parent = FPSObject.gameObject.transform;
			FPSEquipment = gameObject.GetComponent<FPSItemEquipment>();
			FPSObject.SendMessage("OnAttached", gameObject, SendMessageOptions.DontRequireReceiver);
		}
	}

	public void AttachFPSItemViewAndCollector(FPSItemEquipment item, ItemCollector itemcollector)
	{
		NetworkUpdateCount = UpdateCount + 1;
		if (item != null && FPSObject != null)
		{
			Quaternion rotation = FPSObject.transform.rotation;
			rotation.eulerAngles += FPSObject.RotationOffset;
			RemoveEquippedItem(FPSObject);
			GameObject gameObject = Object.Instantiate(item.gameObject, FPSObject.transform.position, rotation);
			gameObject.transform.parent = FPSObject.gameObject.transform;
			FPSEquipment = gameObject.GetComponent<FPSItemEquipment>();
			collectorAttachedTemp = itemcollector;
			FPSObject.SendMessage("OnAttached", gameObject, SendMessageOptions.DontRequireReceiver);
			FPSObject.GetComponent<ItemSticker>().itemCollector = itemcollector;
			SaveDataToItemCollector(FPSEquipment.GetComponent<FPSWeaponEquipment>(), itemcollector);
		}
	}

	public void IsPrimaryCollectorStillExist()
	{
		for (int i = 0; i < Items.Count; i++)
		{
			if (Items[i] != null && Items[i].InUse && Items[i].Num > 0)
			{
				return;
			}
		}
		RemoveEquippedItem(FPSObject);
	}

	public void RemoveEquipItemByCollector(ItemCollector item)
	{
		ItemSticker stickerByItemCollector = GetStickerByItemCollector(item);
		if (stickerByItemCollector != null)
		{
			RemovePrimaryEquip(item);
			UnEquippedItem(stickerByItemCollector);
			clearEquippedIndex(stickerByItemCollector.Index);
			NetworkUpdateCount = UpdateCount + 1;
		}
	}

	public void RemoveSticker(ItemSticker sticker)
	{
		NetworkUpdateCount = UpdateCount + 1;
		if (sticker != null)
		{
			sticker.ItemIndex = -1;
			Component[] componentsInChildren = sticker.transform.GetComponentsInChildren(typeof(ItemEquipment));
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				UnityEngine.Object.Destroy(componentsInChildren[i].gameObject);
			}
		}
	}

	public void RemovePrimaryEquip(ItemCollector item)
	{
		for (int i = 0; i < itemStickers.Length; i++)
		{
			if (itemStickers[i].equipType == EquipType.PrimaryUse && itemStickers[i].itemCollector == item)
			{
				itemStickers[i].ItemIndex = -1;
				Component[] componentsInChildren = itemStickers[i].transform.GetComponentsInChildren(typeof(ItemEquipment));
				for (int j = 0; j < componentsInChildren.Length; j++)
				{
					UnityEngine.Object.Destroy(componentsInChildren[j].gameObject);
				}
			}
		}
	}

	public void UnEquippedItem(ItemSticker sticker)
	{
		NetworkUpdateCount = UpdateCount + 1;
		if (!(sticker != null))
		{
			return;
		}
		if (FPSObject != null)
		{
			ItemSticker component = FPSObject.GetComponent<ItemSticker>();
			if (component != null && component.itemCollector != null && component.itemCollector.EquipIndex == sticker.Index)
			{
				Component[] componentsInChildren = FPSObject.transform.GetComponentsInChildren(typeof(FPSItemEquipment));
				for (int i = 0; i < componentsInChildren.Length; i++)
				{
					componentsInChildren[i].transform.SetParent(null);
					UnityEngine.Object.Destroy(componentsInChildren[i].gameObject);
				}
			}
		}
		Component[] componentsInChildren2 = sticker.transform.GetComponentsInChildren(typeof(ItemEquipment));
		for (int j = 0; j < componentsInChildren2.Length; j++)
		{
			componentsInChildren2[j].transform.SetParent(null);
			UnityEngine.Object.Destroy(componentsInChildren2[j].gameObject);
		}
		sticker.ItemIndex = -1;
	}

	public void RemoveEquippedItem(ItemSticker sticker)
	{
		NetworkUpdateCount = UpdateCount + 1;
		if (!(sticker != null))
		{
			return;
		}
		sticker.ItemIndex = -1;
		Component[] componentsInChildren = sticker.transform.GetComponentsInChildren(typeof(ItemEquipment));
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			componentsInChildren[i].transform.SetParent(null);
			UnityEngine.Object.Destroy(componentsInChildren[i].gameObject);
		}
		if (FPSObject != null)
		{
			Component[] componentsInChildren2 = FPSObject.transform.GetComponentsInChildren(typeof(FPSItemEquipment));
			for (int j = 0; j < componentsInChildren2.Length; j++)
			{
				componentsInChildren2[j].transform.SetParent(null);
				UnityEngine.Object.Destroy(componentsInChildren2[j].gameObject);
			}
		}
	}

	public int GetCollectorFPSindex()
	{
		for (int i = 0; i < Items.Count; i++)
		{
			if (Items[i] != null && Items[i] == collectorAttachedTemp)
			{
				return i;
			}
		}
		return 0;
	}

	public ItemCollector GetItemCollectorByItemIndex(int index)
	{
		for (int i = 0; i < Items.Count; i++)
		{
			if (Items[i].ItemIndex == index)
			{
				return Items[i];
			}
		}
		return null;
	}

	public void SaveDataToItemCollector(FPSWeaponEquipment fpsprefab, ItemCollector item)
	{
		if (fpsprefab != null)
		{
			fpsprefab.SetCollectorSlot(item);
		}
	}

	public void SwarpShortcut(ItemCollector item1, ItemCollector item2)
	{
		ItemCollector itemCollector = new ItemCollector();
		CopyShortcut(itemCollector, item1);
		CopyShortcut(item1, item2);
		CopyShortcut(item2, itemCollector);
		NetworkUpdateCount = UpdateCount + 1;
	}

	public void CopyShortcut(ItemCollector item, ItemCollector source)
	{
		if (item != null && source != null)
		{
			item.Shortcut = source.Shortcut;
		}
	}

	public void PutCollector(ItemCollector item, int invindex)
	{
		putCollector(item.Item.ItemID, invindex, item.Num, item.NumTag);
		NetworkUpdateCount = UpdateCount + 1;
	}

	public void PutCollectorSync(ItemCollector item, int invindex)
	{
		CallCmdPutCollector(item.Item.ItemID, invindex, item.Num, item.NumTag);
	}

	[Command(channel = 0)]
	private void CmdPutCollector(string itemid, int invid, int num, int numtag)
	{
		if ((bool)PeerTrade)
		{
			PeerTrade.putCollector(itemid, invid, num, numtag);
		}
	}

	private void putCollector(string itemid, int invid, int num, int numtag)
	{
		Items[invid].Item = UnitZ.itemManager.CloneItemDataByIndex(itemid);
		Items[invid].Num = num;
		Items[invid].NumTag = numtag;
		Items[invid].Shortcut = -1;
		NetworkUpdateCount = UpdateCount + 1;
	}

	public void SwarpCollector(ItemCollector item1, ItemCollector item2)
	{
		ItemCollector itemCollector = new ItemCollector();
		CopyCollector(itemCollector, item1);
		CopyCollector(item1, item2);
		CopyCollector(item2, itemCollector);
		NetworkUpdateCount = UpdateCount + 1;
	}

	public void CopyCollector(ItemCollector item, ItemCollector source)
	{
		item.Active = source.Active;
		item.ItemIndex = source.ItemIndex;
		item.Item = source.Item;
		item.Num = source.Num;
		item.NumTag = source.NumTag;
		item.Shortcut = source.Shortcut;
		item.EquipIndex = source.EquipIndex;
	}

	public ItemCollector GetItemCollectorByShortCutIndex(int index)
	{
		if (UnitZ.itemManager != null)
		{
			foreach (ItemCollector item in Items)
			{
				if (item != null && item.Shortcut == index)
				{
					return item;
				}
			}
		}
		return null;
	}

	public ItemCollector GetItemCollectorByEquipIndex(int index)
	{
		if (UnitZ.itemManager != null)
		{
			foreach (ItemCollector item in Items)
			{
				if (item != null && item.EquipIndex == index)
				{
					return item;
				}
			}
		}
		return null;
	}

	public ItemCollector GetLatestItemCollector()
	{
		for (int num = Items.Count - 1; num >= 0; num--)
		{
			if (Items[num] != null)
			{
				return Items[num];
			}
		}
		return null;
	}

	public void DeleteShortcut(ItemCollector itemCollector, int shortcut)
	{
		if (UnitZ.itemManager != null)
		{
			foreach (ItemCollector item in Items)
			{
				if (item != null)
				{
					if (itemCollector != item && item.Shortcut == shortcut)
					{
						item.Shortcut = -1;
					}
					if (itemCollector == item)
					{
						item.Shortcut = shortcut;
					}
				}
			}
		}
	}

	private void Update()
	{
		int num = 0;
		foreach (ItemCollector item in Items)
		{
			item.InventoryIndex = num;
			num++;
		}
		if (base.isLocalPlayer && IsReady && character.isSeeAround && LateUpdateCount != UpdateCount)
		{
			string text = GenStickerTextData();
			CallCmdUpdateOtherInventory(text);
			LateUpdateCount = UpdateCount;
		}
		if (!IsReady && StickerTextData != GenStickerTextData())
		{
			onReceivedStickers(StickerTextData);
		}
	}

	[Command(channel = 1)]
	public void CmdUpdateOtherInventory(string text)
	{
		if (text != StickerTextData)
		{
			NetworkStickerTextData = text;
		}
	}

	public void UpdateStickerToServer(string text)
	{
		NetworkStickerTextData = text;
		CallCmdUpdateOtherInventory(StickerTextData);
		IsReady = true;
	}

	public void ApplyStarterItem()
	{
		NetworkUpdateCount = UpdateCount + 1;
		IsReady = true;
		SetupStarterItem();
		UpdateInventoryToAll();
	}

	public void UpdateInventoryToAll()
	{
		IsReady = true;
		string text = GenStickerTextData();
		CallCmdUpdateOtherInventory(text);
	}

	public void OnInventoryChanged(string text)
	{
		NetworkStickerTextData = text;
		if (!base.isLocalPlayer)
		{
			onReceivedStickers(text);
		}
	}

	private void onReceivedStickers(string text)
	{
		if (text == string.Empty)
		{
			return;
		}
		IsReady = true;
		string[] array = text.Split(","[0]);
		for (int i = 0; i < array.Length; i++)
		{
			if (!(array[i] != string.Empty))
			{
				continue;
			}
			int result = 2;
			if (int.TryParse(array[i], out result) && UnitZ.itemManager != null && i < itemStickers.Length && i >= 0)
			{
				ItemData item = UnitZ.itemManager.GetItem(result);
				if (item != null)
				{
					AttachSticker(itemStickers[i], item.ItemEquip);
					itemStickers[i].ItemIndex = result;
				}
				if (result == -1)
				{
					RemoveSticker(itemStickers[i]);
				}
			}
		}
	}

	private void LateUpdate()
	{
		FreeHandsChecker();
	}

	public bool IsFreeHanded()
	{
		if (FPSObject != null && FPSObject != null && FPSObject.gameObject.transform.childCount <= 0)
		{
			return true;
		}
		return false;
	}

	public void FreeHandsChecker()
	{
		if (IsFreeHanded())
		{
			MeleeMode();
		}
	}

	public void MeleeMode()
	{
		if (!(DefaultHand == null))
		{
			AttachFPSItemView(DefaultHand);
		}
	}

	public void EquipmentOnAction(Vector3 direction, byte num, byte spread, byte seed)
	{
		if ((bool)TDEquipment)
		{
			TDEquipment.Action(direction, num, spread, seed);
		}
	}

	public ItemCollector[] GetAllItemAround(float radius)
	{
		List<ItemCollector> list = new List<ItemCollector>();
		GameObject[] array = GameObject.FindGameObjectsWithTag("Item");
		for (int i = 0; i < array.Length; i++)
		{
			if (Vector3.Distance(base.transform.position, array[i].transform.position) <= radius)
			{
				ItemCollector itemCollector = new ItemCollector();
				ItemData itemData = itemCollector.Item = array[i].GetComponent<ItemData>();
				itemCollector.ItemIndex = itemData.ItemIndex;
				itemCollector.Num = itemData.Quantity;
				itemCollector.NumTag = itemData.NumTag;
				list.Add(itemCollector);
			}
		}
		return list.ToArray();
	}

	public void PickUpAllAround(float radius)
	{
		GameObject[] array = GameObject.FindGameObjectsWithTag("Item");
		for (int i = 0; i < array.Length; i++)
		{
			if (Vector3.Distance(base.transform.position, array[i].transform.position) <= radius)
			{
				array[i].SendMessage("Pickup", base.gameObject, SendMessageOptions.DontRequireReceiver);
			}
		}
	}

	private void UNetVersion()
	{
	}

	protected static void InvokeCmdCmdAddItemSync(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdAddItemSync called on client.");
		}
		else
		{
			((CharacterInventory)obj).CmdAddItemSync(reader.ReadString(), (int)reader.ReadPackedUInt32(), (int)reader.ReadPackedUInt32(), (int)reader.ReadPackedUInt32());
		}
	}

	protected static void InvokeCmdCmdRemoveItemSync(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdRemoveItemSync called on client.");
		}
		else
		{
			((CharacterInventory)obj).CmdRemoveItemSync((int)reader.ReadPackedUInt32(), (int)reader.ReadPackedUInt32());
		}
	}

	protected static void InvokeCmdCmdDropitem(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdDropitem called on client.");
		}
		else
		{
			((CharacterInventory)obj).CmdDropitem(reader.ReadString(), (int)reader.ReadPackedUInt32(), (int)reader.ReadPackedUInt32(), reader.ReadVector3(), reader.ReadQuaternion());
		}
	}

	protected static void InvokeCmdCmdPutCollector(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdPutCollector called on client.");
		}
		else
		{
			((CharacterInventory)obj).CmdPutCollector(reader.ReadString(), (int)reader.ReadPackedUInt32(), (int)reader.ReadPackedUInt32(), (int)reader.ReadPackedUInt32());
		}
	}

	protected static void InvokeCmdCmdUpdateOtherInventory(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdUpdateOtherInventory called on client.");
		}
		else
		{
			((CharacterInventory)obj).CmdUpdateOtherInventory(reader.ReadString());
		}
	}

	public void CallCmdAddItemSync(string itemid, int num, int numtag, int shortcut)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdAddItemSync called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdAddItemSync(itemid, num, numtag, shortcut);
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdAddItemSync);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.Write(itemid);
		networkWriter.WritePackedUInt32((uint)num);
		networkWriter.WritePackedUInt32((uint)numtag);
		networkWriter.WritePackedUInt32((uint)shortcut);
		SendCommandInternal(networkWriter, 0, "CmdAddItemSync");
	}

	public void CallCmdRemoveItemSync(int index, int num)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdRemoveItemSync called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdRemoveItemSync(index, num);
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdRemoveItemSync);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.WritePackedUInt32((uint)index);
		networkWriter.WritePackedUInt32((uint)num);
		SendCommandInternal(networkWriter, 0, "CmdRemoveItemSync");
	}

	public void CallCmdDropitem(string itemid, int numtag, int num, Vector3 position, Quaternion rotation)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdDropitem called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdDropitem(itemid, numtag, num, position, rotation);
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdDropitem);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.Write(itemid);
		networkWriter.WritePackedUInt32((uint)numtag);
		networkWriter.WritePackedUInt32((uint)num);
		networkWriter.Write(position);
		networkWriter.Write(rotation);
		SendCommandInternal(networkWriter, 0, "CmdDropitem");
	}

	public void CallCmdPutCollector(string itemid, int invid, int num, int numtag)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdPutCollector called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdPutCollector(itemid, invid, num, numtag);
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdPutCollector);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.Write(itemid);
		networkWriter.WritePackedUInt32((uint)invid);
		networkWriter.WritePackedUInt32((uint)num);
		networkWriter.WritePackedUInt32((uint)numtag);
		SendCommandInternal(networkWriter, 0, "CmdPutCollector");
	}

	public void CallCmdUpdateOtherInventory(string text)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdUpdateOtherInventory called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdUpdateOtherInventory(text);
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdUpdateOtherInventory);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.Write(text);
		SendCommandInternal(networkWriter, 1, "CmdUpdateOtherInventory");
	}

	public override float GetNetworkSendInterval()
	{
		return 1f;
	}

	static CharacterInventory()
	{
		kCmdCmdAddItemSync = -1553260676;
		NetworkBehaviour.RegisterCommandDelegate(typeof(CharacterInventory), kCmdCmdAddItemSync, InvokeCmdCmdAddItemSync);
		kCmdCmdRemoveItemSync = 2027250277;
		NetworkBehaviour.RegisterCommandDelegate(typeof(CharacterInventory), kCmdCmdRemoveItemSync, InvokeCmdCmdRemoveItemSync);
		kCmdCmdDropitem = -868225483;
		NetworkBehaviour.RegisterCommandDelegate(typeof(CharacterInventory), kCmdCmdDropitem, InvokeCmdCmdDropitem);
		kCmdCmdPutCollector = 1437760625;
		NetworkBehaviour.RegisterCommandDelegate(typeof(CharacterInventory), kCmdCmdPutCollector, InvokeCmdCmdPutCollector);
		kCmdCmdUpdateOtherInventory = 1630043272;
		NetworkBehaviour.RegisterCommandDelegate(typeof(CharacterInventory), kCmdCmdUpdateOtherInventory, InvokeCmdCmdUpdateOtherInventory);
		NetworkCRC.RegisterBehaviour("CharacterInventory", 0);
	}

	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		if (forceAll)
		{
			writer.Write(StickerTextData);
			writer.WritePackedUInt32((uint)UpdateCount);
			return true;
		}
		bool flag = false;
		if ((base.syncVarDirtyBits & 1) != 0)
		{
			if (!flag)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag = true;
			}
			writer.Write(StickerTextData);
		}
		if ((base.syncVarDirtyBits & 2) != 0)
		{
			if (!flag)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag = true;
			}
			writer.WritePackedUInt32((uint)UpdateCount);
		}
		if (!flag)
		{
			writer.WritePackedUInt32(base.syncVarDirtyBits);
		}
		return flag;
	}

	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
		if (initialState)
		{
			StickerTextData = reader.ReadString();
			UpdateCount = (int)reader.ReadPackedUInt32();
			return;
		}
		int num = (int)reader.ReadPackedUInt32();
		if ((num & 1) != 0)
		{
			OnInventoryChanged(reader.ReadString());
		}
		if ((num & 2) != 0)
		{
			UpdateCount = (int)reader.ReadPackedUInt32();
		}
	}
}
